"""
课程模型
"""
from datetime import datetime
from app.extensions import db


class Course(db.Model):
    """课程模型"""
    __tablename__ = 'courses'
    
    id = db.Column(db.Integer, primary_key=True)
    code = db.Column(db.String(20), unique=True, nullable=False, index=True)
    name = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text)
    credits = db.Column(db.Float, nullable=False)
    department = db.Column(db.String(100))
    instructor_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    semester = db.Column(db.String(50))
    academic_year = db.Column(db.String(20))  # 学年，如 "2023-2024"
    semester_term = db.Column(db.Integer)  # 学期，1=第一学期，2=第二学期
    course_category = db.Column(db.String(50))  # 课程类别
    class_number = db.Column(db.String(20))  # 教学班号
    max_students = db.Column(db.Integer)
    classroom = db.Column(db.String(50))
    schedule = db.Column(db.String(200))
    status = db.Column(db.String(20), default='active')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 关系
    instructor = db.relationship('User', backref='courses_taught', foreign_keys=[instructor_id])
    student_courses = db.relationship('StudentCourse', back_populates='course', cascade='all, delete-orphan')
    tags = db.relationship('CourseTag', backref='course', cascade='all, delete-orphan')
    prerequisites = db.relationship('CoursePrerequisite', 
                                   foreign_keys='CoursePrerequisite.course_id',
                                   backref='course',
                                   cascade='all, delete-orphan')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'code': self.code,
            'name': self.name,
            'description': self.description,
            'credits': self.credits,
            'department': self.department,
            'instructor_id': self.instructor_id,
            'instructor_name': f"{self.instructor.last_name}{self.instructor.first_name}" if self.instructor else None,
            'semester': self.semester,
            'academic_year': self.academic_year,
            'semester_term': self.semester_term,
            'course_category': self.course_category,
            'class_number': self.class_number,
            'max_students': self.max_students,
            'classroom': self.classroom,
            'schedule': self.schedule,
            'status': self.status,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'tags': [tag.tag for tag in self.tags]
        }


class CoursePrerequisite(db.Model):
    """课程前置要求模型"""
    __tablename__ = 'course_prerequisites'
    
    id = db.Column(db.Integer, primary_key=True)
    course_id = db.Column(db.Integer, db.ForeignKey('courses.id'), nullable=False)
    prerequisite_id = db.Column(db.Integer, db.ForeignKey('courses.id'), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    prerequisite = db.relationship('Course', foreign_keys=[prerequisite_id])


class CourseTag(db.Model):
    """课程标签模型"""
    __tablename__ = 'course_tags'
    
    id = db.Column(db.Integer, primary_key=True)
    course_id = db.Column(db.Integer, db.ForeignKey('courses.id'), nullable=False)
    tag = db.Column(db.String(50), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
